<?php

namespace App\Providers;

use App\Model\Category;
use App\Model\Globals;
use App\Models\Archive\Archive;
use App\Models\Tag\Tag;
use App\Observers\TagObserver;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use App\Model\Links;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Tag::observe(TagObserver::class);
        $about = Category::where('cate_name','关于我们')->first();
        $cate = Category::where('cate_pid',$about->cate_id)->orderBy('cate_weight','asc')->take(3)->get();

        $about = Category::where('cate_name','奇异果资讯')->first();
        $qyg = Category::where('cate_pid',$about->cate_id)->orderBy('cate_weight','asc')->take(3)->get();
        app('view')->composer('home.inc.common', function ($view) use ($cate,$qyg) {
            $view->with('links', Links::where('is_on',1)->orderBy('id', 'desc')->take(10)->get())
                ->with('top_cate',Category::where('cate_pid',0)->where('cate_weight','<>',99)->orderBy('cate_weight','asc')->get())
                ->with('global',Globals::first())
                ->with('about',$cate)
                ->with('qyg',$qyg)
            ;
        });

        app('view')->composer('mobile.inc.common', function ($view) use ($cate,$qyg) {
            $view->with('links', Links::where('is_on',1)->orderBy('id', 'desc')->take(10)->get())
                ->with('top_cate',Category::where('cate_pid',0)->where('cate_weight','<>',99)->orderBy('cate_weight','asc')->get())
                ->with('global',Globals::first())
                ->with('about',$cate)
                ->with('qyg',$qyg)
            ;
        });
        app('view')->composer('mobile.anchor.common', function ($view) use ($cate,$qyg) {
            $view->with('links', Links::where('is_on',1)->orderBy('id', 'desc')->take(10)->get())
                ->with('top_cate',Category::where('cate_pid',0)->where('cate_weight','<>',99)->orderBy('cate_weight','asc')->get())
                ->with('global',Globals::first())
                ->with('about',$cate)
                ->with('qyg',$qyg)
            ;
        });
        app('view')->composer('home.index.index', function ($view) use ($cate,$qyg) {
            $view->with('links', Links::where('is_on',1)->orderBy('id', 'desc')->take(10)->get())
                ->with('top_cate',Category::where('cate_pid',0)->where('cate_weight','<>',99)->orderBy('cate_weight','asc')->get())
                ->with('global',Globals::first())
                ->with('about',$cate)
                ->with('qyg',$qyg)
            ;
        });
        app('view')->composer('home.search.layout', function ($view) {
            $view->with('top_cate',Category::where('cate_pid',0)->where('cate_weight','<>',99)->orderBy('cate_weight','asc')->get())
                ->with('global',Globals::first())
            ;
        });
        app('view')->composer('home.search.index', function ($view) {
            $view->with('top_cate',Category::where('cate_pid',0)->where('cate_weight','<>',99)->orderBy('cate_weight','asc')->get())
                ->with('global',Globals::first())
            ;
        });

        app('view')->composer('home.about.layouts',function($view)use($cate,$qyg){
            $view->with('top_cate',Category::where('cate_pid',0)->where('cate_weight','<>',99)->orderBy('cate_weight','asc')->get())
                ->with('global',Globals::first())
                ->with('about',$cate)
                ->with('qyg',$qyg)
            ;
        });
        app('view')->composer('home.qyg.layouts',function($view)use($cate,$qyg){
            $view->with('top_cate',Category::where('cate_pid',0)->where('cate_weight','<>',99)->orderBy('cate_weight','asc')->get())
                ->with('global',Globals::first())
                ->with('about',$cate)
                ->with('qyg',$qyg)
            ;
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
    }

    private function getRecommend($cates)
    {
        $result = [];
        foreach ($cates as $cate) {
            $cate = Category::where('cate_name',$cate)->first();
            if ($cate) {
                $result[$cate->cate_name] = Archive::where('category_id', $cate->cate_id)->take(6)->get();
            }
        }
        return $result;
    }
}
